package com.qf.java2106.service.impl;


import com.qf.java2106.common.ResultVO;
import com.qf.java2106.constant.MessageConstant;
import com.qf.java2106.dao.IUserDao;
import com.qf.java2106.pojos.User;
import com.qf.java2106.service.IUserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

import java.sql.SQLException;

/**
 * @author q
 * @version V1.0
 * @date 2021/10/28
 **/
public class UserServiceImpl implements IUserService {

    /*ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    IUserDao userDao = (IUserDao) applicationContext.getBean("userDao");*/

    private IUserDao userDao;

    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public ResultVO userLogin(User loginUser) {
        try {
            Integer isAdmin = userDao.userLogin(loginUser);
            if (null==isAdmin){
                return ResultVO.error("用户名或密码错误~");
            }
            if (!(loginUser.getIsAdmin()==isAdmin.intValue())){
                return ResultVO.error("您没有该系统权限，请切换系统登陆~");
            }
            return ResultVO.ok("登陆成功~",loginUser);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @Override
    public ResultVO userCheck(String username) {
        try {
            if (StringUtils.isEmpty(username)){
                return ResultVO.error(MessageConstant.USERNAME_IS_NOT_NULL);
            }
            User user = userDao.findUserByUsername(username);
            if (null == user){
                return ResultVO.error(MessageConstant.USERNAME_IS_NOT_EXISTS_LOGIN);
            } else {
                return ResultVO.ok(MessageConstant.USERNAME_IS_EXISTS_LOGIN,user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @Override
    public ResultVO userLogon(User logonUser) {
        try {
            return ResultVO.ok("注册成功,请登陆~",userDao.userLogon(logonUser));
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error("注册失败~",e.getMessage());
        }
    }

    @Override
    public ResultVO userLogonCheck(String userName) {
        try {
            if (StringUtils.isEmpty(userName)){
                return ResultVO.error(MessageConstant.USERNAME_IS_NOT_NULL);
            }
            User user = userDao.findUserByUsername(userName);
            if (null == user){
                return ResultVO.ok(MessageConstant.USERNAME_IS_NOT_EXISTS);
            } else {
                return ResultVO.error(MessageConstant.USERNAME_IS_EXISTS,user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }
}
